import Ajax from "../../utils/ajax"
const app = getApp();

Page({
 
  data: {

  },
  login(){
    var th = this;
    wx.getUserInfo({
      success: function(res) {
        var { nickName,avatarUrl } = res.userInfo
        console.log("res.userInfo",res.userInfo);
        wx.setStorageSync('userInfo', res.userInfo);
        app.globalData.userInfo = res.userInfo
        wx.login({
          success (res) {
            var { code } = res
            console.log(code);
            if (code) { 
              Ajax.get(Ajax.api.wxLogin,{  appid:"wx9ada1ca5bc5f4063", code,  nickName, avatarUrl,}).then(res=>{
                  console.log("登录后的响应",res);
                  app.globalData.token ='Bearer ' + res.data.token;
                  Ajax.get(Ajax.api.getUserInfo,{  }).then((res)=>{
                    console.log(res)
                    app.globalData.getUserInfo = res.data
                    
                    th.setData({   user:  res.data  });
                    wx.navigateTo({ url:'/views/home/index' })
                  });
                  
               })
            } else {
              console.log('登录失败！' + res.errMsg)
            }
          }
        }) 
      }
    })
  },
  onLoad: function (options) {
     setTimeout(()=>{
      this.login()
     },2000)
  }, 
  onReady: function () {

  },
 
  onShow: function () {

  },
 
  onHide: function () {

  }, 
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})